//
//  SetupViewController.m
//  ShiWuPai
//
//  Created by macmacpro on 16/11/23.
//  Copyright (c) 2016年 QQ:244987427. All rights reserved.
//

#import "SetupViewController.h"

#define ALERT(msg) [[[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show]

@interface SetupViewController ()<UITableViewDelegate,UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *cacheLabel;
@property (strong, nonatomic) IBOutlet UITableView *setupTabView;


@end

@implementation SetupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"设置";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"ic_back_dark_20x22_@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(goPersonal)];
    self.cacheLabel.text = [NSString stringWithFormat:@"%.2fM",[self filePath]];
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.scrollEnabled = NO;
    self.tabBarController.tabBar.hidden = YES;
    
}


- (void)goPersonal{
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}


#pragma mark - 清理缓存

//1:首先我们计算一下 单个文件的大小
- (long long)fileSizeAtPath:( NSString *)filePath{
    NSFileManager * manager= [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

//2: 遍历文件夹获得文件夹大小，返回多少 M
- (float)folderSizeAtPath:(NSString *)folderPath{
    NSFileManager *manager= [NSFileManager defaultManager];
    if (![manager fileExistsAtPath :folderPath])return 0;
    NSEnumerator *childFilesEnumerator= [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString *fileName;
    long long folderSize = 0 ;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString *fileAbsolutePath= [folderPath stringByAppendingPathComponent :fileName];
        folderSize +=[self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0 * 1024.0);
}


// 显示缓存大小
- (float)filePath{
    NSString *cachPath= [ NSSearchPathForDirectoriesInDomains (NSCachesDirectory,NSUserDomainMask,YES) firstObject];
    return[self folderSizeAtPath:cachPath];
}

// 清理缓存
- (void)clearFile{
    NSString * cachPath= [NSSearchPathForDirectoriesInDomains (NSCachesDirectory,NSUserDomainMask,YES) firstObject];
    
    NSArray * files= [[NSFileManager defaultManager] subpathsAtPath:cachPath];
    //    NSLog (@"cachpath = %@",cachPath);
    for (NSString *p in files) {
        NSError *error = nil ;
        NSString *path = [cachPath stringByAppendingPathComponent :p];
        if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) {
            [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error];
        }
    }
    [self performSelectorOnMainThread:@selector(clearCachSuccess) withObject :nil waitUntilDone:YES];
}

- (void)clearCachSuccess{
    ALERT(@"缓存清理完毕");
    self.cacheLabel.text = [NSString stringWithFormat:@"%.2fM",[self filePath]];//清理完之后重新导入数据
}


#pragma mark -UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确定清除本地缓存图片吗？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }
    if (indexPath.row == 2) {
        ALERT(@"谢谢你的建议!");
    }
    if (indexPath.row == 3) {
        ALERT(@"谢谢你的评分!");
    }
    if (indexPath.row == 4) {
        ALERT(@"谢谢你的分享!");
    }
    if (indexPath.row == 5) {
        ALERT(@"一种神奇未知的功能!?");
    }
}

#pragma mark -UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 1:
            [self clearFile];
            break;
        default:
            break;
    }
}


@end
